<template>
  <div class="score-list">

    <!-- 列表项 -->
    <div class="item-list">
      <div
          v-for="item in data.tableData"
          :key="item.id"
          class="item-card"
      >
        <!-- 用户信息 -->
        <div class="user">
          <span class="user-name">{{ item.userName || '匿名用户' }}</span>
        </div>

        <!-- 壁纸预览 -->
        <div class="image-wrapper">
          <el-image
              v-if="item.goodsImage"
              :src="item.goodsImage"
              fit="cover"
              :preview-src-list="[item.goodsImage]"
              preview-teleported
              class="wallpaper-image"
          />
          <div v-else class="no-image">无图片</div>
        </div>

        <!-- 操作按钮 -->
        <div class="actions">
          <el-button
              type="danger"
              size="small"
              :icon="Delete"
              circle
              @click="del(item.id)"
              :title="`删除 ${item.userName} 的下载记录`"
          />
        </div>
      </div>

      <!-- 空数据 -->
      <div v-if="data.tableData.length === 0" class="empty">
        <el-empty description="暂无下载记录"/>
      </div>
    </div>

    <!-- 分页 -->
    <div v-if="data.total > 0" class="pagination-container">
      <el-pagination
          @current-change="load"
          background
          layout="prev, pager, next"
          :page-size="data.pageSize"
          v-model:current-page="data.pageNum"
          :total="data.total"
      />
    </div>
  </div>
</template>

<script setup>
import {reactive, onMounted} from 'vue';
import request from '@/utils/request.js';
import {ElMessage, ElMessageBox} from 'element-plus';
import {Delete} from '@element-plus/icons-vue';

const data = reactive({
  tableData: [],
  pageNum: 1,
  pageSize: 3,
  total: 0,
});

// 加载数据
const load = () => {
  request
      .get('/score/selectPage', {
        params: {
          pageNum: data.pageNum,
          pageSize: data.pageSize,
        },
      })
      .then((res) => {
        if (res.code === '200') {
          data.tableData = res.data?.list || [];
          data.total = res.data?.total || 0;
        } else {
          ElMessage.error(res.msg || '加载失败');
        }
      })
      .catch(() => {
        ElMessage.error('网络错误，加载数据失败');
      });
};

// 删除单条记录
const del = (id) => {
  ElMessageBox.confirm('删除后数据无法恢复，您确定删除吗？', '删除确认', {
    type: 'warning',
    confirmButtonText: '确定删除',
    cancelButtonText: '取消',
  })
      .then(() => {
        request.delete(`/score/delete/${id}`).then((res) => {
          if (res.code === '200') {
            ElMessage.success('删除成功');
            load(); // 刷新列表
          } else {
            ElMessage.error(res.msg || '删除失败');
          }
        });
      })
      .catch(() => {
        // 取消删除
      });
};

// 页面初始化加载
onMounted(() => {
  load();
});
</script>

<style scoped lang="scss">
// 主题变量
$primary-color: #4361ee;
$danger-color: #f72585;
$border-color: #e9ecef;
$bg-color: #f8f9fa;
$text-light: #6c757d;
$text-dark: #212529;
$card-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
$card-hover-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
$border-radius: 12px;
$transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);

// 布局容器
.score-list {
  padding: 24px;
  background-color: $bg-color;
  min-height: 100vh;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color: $text-dark;
}

/* 列表项容器 - Flex 布局，自动换行 */
.item-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 24px;
}

/* 卡片整体样式 */
.item-card {
  display: flex;
  flex-direction: column;
  width: calc(33.333% - 13.33px); // 三列，考虑 gap: 20px
  background: #fff;
  border-radius: $border-radius;
  box-shadow: $card-shadow;
  border: 1px solid $border-color;
  transition: $transition;
  overflow: hidden;

  &:hover {
    box-shadow: $card-hover-shadow;
    transform: translateY(-6px);
    border-color: mix($primary-color, $border-color, 30%);
  }
}

/* 用户信息 */
.user {
  padding: 12px 16px 8px;

  .user-name {
    font-weight: 600;
    font-size: 15px;
    color: $text-dark;
    background-color: rgba($primary-color, 0.1);
    color: $primary-color;
    padding: 6px 10px;
    border-radius: 6px;
    font-weight: 500;
  }
}

/* 壁纸预览区域 */
.image-wrapper {
  position: relative;
  width: 100%;
  height: 180px; // 固定高度，避免布局跳动
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  overflow: hidden;

  .wallpaper-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: zoom-in;
    transition: transform 0.3s ease;

    &:hover {
      transform: scale(1.05);
    }
  }
}

.no-image {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 180px;
  background: repeating-linear-gradient(
          45deg,
          #f8f9fa,
          #f8f9fa 10px,
          #e9ecef 10px,
          #e9ecef 20px
  );
  color: $text-light;
  font-size: 14px;
  font-style: italic;
}

/* 操作按钮区域 */
.actions {
  padding: 12px 16px;
  display: flex;
  justify-content: flex-end;

  .el-button {
    padding: 8px;
    border-radius: 50%;
    transition: $transition;

    &:hover {
      transform: scale(1.15);
      background-color: $danger-color !important;
      color: white !important;
    }

    &:active {
      transform: scale(1);
    }
  }
}

/* 空状态样式 */
.empty {
  grid-column: 1 / -1;
  padding: 60px 0;
  text-align: center;

  :deep(.el-empty__description p) {
    color: $text-light;
    font-size: 15px;
    margin-top: 8px;
  }
}

/* 分页样式 */
.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;

  :deep(.el-pagination) {
    .el-pager li {
      &.is-active {
        background-color: $primary-color !important;
        color: white !important;
        border-radius: 6px;
      }

      &:hover:not(.is-disabled) {
        color: $primary-color;
      }
    }

    button {
      &:hover {
        color: $primary-color;
      }
    }
  }
}

/* 响应式布局 */
@media (max-width: 1024px) {
  .item-card {
    width: calc(50% - 10px); // 两列
  }
}

@media (max-width: 768px) {
  .score-list {
    padding: 16px;
  }

  .item-card {
    width: 100%; // 单列
  }

  .user {
    .user-name {
      font-size: 14px;
      padding: 5px 8px;
    }
  }

  .image-wrapper,
  .no-image {
    height: 160px;
  }

  .actions {
    padding: 10px 12px;
  }
}

@media (max-width: 480px) {
  .score-list {
    padding: 12px;
  }

  .image-wrapper,
  .no-image {
    height: 140px;
  }

  .user {
    padding: 10px;
  }

  .actions {
    padding: 10px;
  }
}
</style>